import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import Factory from '../../factory';
import dataTableCreate from '../../addons/DataTable'
import dataEditCreate from '../../addons/DataEdit'
const daoName = "INDI_LSPH_ZJXX";
const ButtonGroup = Button.Group;
const columns = [
  { title: '被转介客户姓名', dataIndex: 'BZJXM', key: 'BZJXM', width: 180, sorter: true, dispaly: true, filter: true, export: true },
  { title: '被转介身份证号', dataIndex: 'BZJIDNO', key: 'BZJIDNO', width: 180, sorter: true, dispaly: true, filter: true, export: true },
  { title: '被转介企业名称', dataIndex: 'BZJJGMC', key: 'BZJJGMC', width: 300, sorter: true, dispaly: true, filter: true, export: true },
  { title: '被转介企业组织机构代码', dataIndex: 'BZJJGDM', key: 'BZJJGDM', width: 220, sorter: true, dispaly: true, filter: true, export: true },
  { title: '推荐客户姓名', dataIndex: 'TJKHXM', key: 'TJKHXM', width: 150, sorter: true, dispaly: true, filter: true, export: true },
  { title: '推荐客户身份证号', dataIndex: 'TJIDNO', key: 'TJIDNO', width: 180, sorter: true, dispaly: true, filter: true, export: true },
  { title: '维护客户经理工号', dataIndex: 'WHKHJLID', key: 'WHKHJLID', width: 180, sorter: true, dispaly: true, filter: true, export: true },
  { title: '维护客户经理姓名', dataIndex: 'WHKHJLXM', key: 'WHKHJLXM', width: 180, sorter: true, dispaly: true, filter: true, export: true },
  { title: '备注信息', dataIndex: 'BZ', key: 'BZ', width: 300, sorter: true, dispaly: true, filter: true, export: true },
];
const controls = [
  { name: 'BZJXM', required: false, ignore: false, type: 'text', label: '被转介客户姓名', grid: 24 / 24 },
  { name: 'BZJIDNO', required: false, ignore: false, type: 'text', label: '被转介身份证号', grid: 24 / 24 },
  { name: 'BZJJGMC', required: false, ignore: false, type: 'text', label: '被转介企业名称', grid: 24 / 24 },
  { name: 'BZJJGDM', required: false, ignore: false, type: 'text', label: '被转介企业组织机构代码', grid: 24 / 24 },
  { name: 'TJKHXM', required: false, ignore: false, type: 'text', label: '推荐客户姓名', grid: 24 / 24 },
  { name: 'TJIDNO', required: true, ignore: false, type: 'text', label: '推荐客户身份证号', grid: 24 / 24 },
  { name: 'WHKHJLID', required: true, ignore: false, type: 'disabled', label: '维护客户经理工号', grid: 24 / 24 },
  { name: 'WHKHJLXM', required: true, ignore: false, type: 'disabled', label: '维护客户经理姓名', grid: 24 / 24 },
  { name: 'BZ', required: false, ignore: false, type: 'text', label: '备注信息', grid: 24 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class GrphzjxxlrForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID" }
    });
    let whereItems = [];
    let WhereCauses = [];
    whereItems.push({
      FieldName:"WHKHJLID",
      Operator: "=",
      FieldValues: this.props.UserNo
    })
    WhereCauses.push({
      WhereItems:whereItems,
      relationSign: "AND"
    })
    let seeks = WhereCauses
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 50 ,seeks}
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showModelHandler(isAdd) {
    let key = null
    if (!isAdd) {
      const len = this.props.selectedRowKeys.length;
      if (len === 0) {
        message.info('请选择要编辑的行，如果选择了多行，则使用最后一次选择！');
        return;
      }
      key = this.props.selectedRowKeys[len - 1]
    }
    this.props.dispatch({
      type: `${daoName}/openEdit`,
      payload: { key }
    });
  }
  edidOkHandler() {
    const form = this.refs.OrganForm.getForm()
    form.validateFields((err, values) => {
      if(values.BZJIDNO=='' && values.BZJJGMC==''){
        message.info('被转介身份证号与被转介企业名称必须存在一个！',5)
        return
      }
      if(values.BZJXM!='' && values.BZJIDNO==''){
        message.info('被转介姓名存在时被转介身份证号必须存在！',5)
        return
      }
      if (!err) {
        let t = `${daoName}/create`
        const { keyName, record } = this.props;
        let msg = "数据添加成功！"
        if (record[keyName]) {
          values[keyName] = record[keyName];
          t = `${daoName}/patch`
          msg = "数据修改成功！"
        }
        controls.map((c, i) => {
          if (c.type === 'cascader-select') {
            let vs = values[c.name];
            if (vs.length > 0) {
              values[c.name] = vs[vs.length - 1];
            } else {
              values[c.name] = null;
            }
          }
          if (c.dataType === "number") {
            values[c.name] = Number(values[c.name])
          } else if (c.dataType === "date" || c.dataType === "datetime") {
            values[c.name] = !values[c.name] ? null : new Date(values[c.name])
          } else {
            values[c.name] = values[c.name] === undefined ? null : values[c.name]
          }
          if (c.multiple || c.type === 'checkbox-group') {
            values[c.name] = values[c.name].join(',');
          }
        })
        values.EMID = this.props.userID
        this.state.dataChange = true;
        this.props.dispatch({
          type: t,
          payload: { daoName, values },
        })
      }
    });
  };
  hideEditHandler() {
    const form = this.refs.OrganForm.getForm();
    form.resetFields();
    if (this.state.dataChange) {
      this.props.dispatch({
        type: `${daoName}/reload`,
        payload: { daoName },
      });
      this.state.dataChange = false;
    }
    this.props.dispatch({
      type: `${daoName}/hideEdit`
    });
  };
  deleteHandler() {
    const keys = this.props.selectedRowKeys;
    const len = keys.length;
    if (len === 0) {
        message.info('请选择要删除的转介信息！', 5);
        return;
    }
    Modal.confirm({
        title: '提示',
        content: `你确认要删除选中的${len}条转介信息吗？`,
        okText: '删除',
        cancelText: '取消',
        onOk: () => {
            this.props.dispatch({
                type: `${daoName}/remove`,
                payload: { daoName, keys },
            });
        }
    });
  };
  render() {
    let { showEdit, record, permID, popedomTypes, UserNo,userName } = this.props
    let data = null
    let pTypes = popedomTypes[permID]
    data = < ButtonGroup style={{ zIndex: 0 }}>
      <Button onClick={this.showModelHandler.bind(this, true)}>新增</Button>
      <Button onClick={this.deleteHandler.bind(this)}>删除</Button>    
      </ButtonGroup >
    record.WHKHJLXM = userName
    record.WHKHJLID = UserNo
    record.BZJIDNO = ''
    record.BZJJGMC = ''
    record.BZJXM = ''
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <Modal title="机构编辑" width={700} visible={showEdit} onOk={this.edidOkHandler.bind(this)} onCancel={this.hideEditHandler.bind(this)}
            footer={[
              <Button key="close" type="ghost" onClick={this.hideEditHandler.bind(this)}>关闭</Button>,
              <Button key="submit" type="primary" onClick={this.edidOkHandler.bind(this)}>保存</Button>
            ]}
          >
            <font style={{ color: "red", fontSize: "14px", float: "left", marginLeft: 40 }}>
              注：被转介身份证号与被转介企业名称必须存在一个，请规范填写
            </font>
            <EditFrom ref="OrganForm" daoName={daoName} controls={controls} record={record} />
          </Modal>
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state["INDI_LSPH_ZJXX"];
  const { userID, orgID, pOrgID, popedomTypes,UserNo, userName } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    UserNo,
    userName,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
GrphzjxxlrForm = connect(mapStateToProps)(GrphzjxxlrForm)
export default GrphzjxxlrForm;

let render = (props) => React.createElement(GrphzjxxlrForm, props);
let f = new Factory()
f.set("GrphzjxxlrForm", render)
